<template>
	<!-- 添加日志 -->
	<view class="building-add">
		<view class="add-content">
			<view class="add-item">
				<view class="item-left">日志标题<text>*</text></view>
				<view class="item-right"><input v-model="title" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入日志标题" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">日志内容<text>*</text></view>
				<view class="item-right"><textarea v-model="content" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入日志内容"></textarea></view>
			</view>
		</view>
		<view class="add-head">
<!-- 			<view class="head-left">
				<view class="head-left-1">上传图片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view> -->
			<view class="head-right" @click="chooseImage">
				<image class="img" v-if="imgs==''" src="../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="imgs"></image>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default{
		data() {
			return{
				imgs:'',
				title:'',
				content:''
			}
		},
		methods:{
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.imgs = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 提交
			handleSubmit() {
				if(this.title == '') {
					return this.$refs.uToast.show({
						title: '请填写日志标题'
					})
				}
				if(this.content == '') {
					return this.$refs.uToast.show({
						title: '请填写日志内容'
					})
				}
				if(this.imgs == '') {
					return this.$refs.uToast.show({
						title: '请上传图片'
					})
				}
				let params = {
					content:this.content,
					imgs:this.imgs,
					title:this.title
				}
				console.log(params)
				this.$u.api.addWorkLog(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'日志添加成功',
								icon:'success'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta:1
								})
							},1200)
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'日志添加失败',
							icon:'none'
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.gridcontent{
			height: 40vh;
			width: 100%;
			padding: 20rpx;
			color: #333;
			font-size: 27rpx;
			text-align: left;
			.item{
				width: 100%;
				padding: 5rpx 40rpx;
				overflow-y: auto;
				.item1{
					padding: 5rpx 40rpx;
					.item2{
						padding: 5rpx 40rpx;
						.item3{
							padding: 5rpx 40rpx;
							.item4{
								padding: 5rpx 40rpx;
								
							}
						}
					}
				}
			}
		}
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 20rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					flex: 4;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
					text{
						display: inline-block;
						margin-left: 5rpx;
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
